package module.service.impl;

import common.bean.SearchPageResult;
import common.bean.SearchParameter;
import module.dao.AssessmentRecordDao;
import module.dao.AssessmentRecordValueDao;
import module.model.AssessmentRecordModel;
import module.model.AssessmentRecordValueModel;
import module.service.AssessmentRecordValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AssessmentRecordValueServiceImpl implements AssessmentRecordValueService {

    @Autowired
    private AssessmentRecordValueDao assessmentRecordValueDao;
    @Autowired
    private AssessmentRecordDao assessmentRecordDao;
    @Transactional
    public SearchPageResult<AssessmentRecordValueModel> getAssessmentRecordValueModelListPage(SearchParameter searchParameter) {
        return assessmentRecordValueDao.getAssessmentRecordValueModelListPage(searchParameter);
    }
    @Transactional
    public void saveListAssessmentRecordValueModel(List<AssessmentRecordValueModel> assessmentRecordValueModels,Long createUserId) {
        boolean isFirst = true;
        Long recordsId = null;
        for(AssessmentRecordValueModel assessmentRecordValueModel : assessmentRecordValueModels){
            if(isFirst){
                AssessmentRecordModel assessmentRecordModel = new AssessmentRecordModel();
                assessmentRecordModel.setCreateUserId(createUserId);
                assessmentRecordModel.setFormId(assessmentRecordValueModel.getFormId());
                assessmentRecordModel.setCreateDate(new Date());
                assessmentRecordModel.setServiceId(assessmentRecordValueModel.getServiceId());
                assessmentRecordModel.setFormName(assessmentRecordValueModel.getFormName());
                assessmentRecordDao.save(assessmentRecordModel);
                recordsId = assessmentRecordModel.getRecordsId();

            }
            if(recordsId!=null) {
                assessmentRecordValueModel.setRecordsId(recordsId);
                assessmentRecordValueModel.setCreateTime(new Date());
                assessmentRecordValueModel.setCreateUserId(createUserId);
                assessmentRecordValueDao.save(assessmentRecordValueModel);
            }
            isFirst = false;
         }
    }
}
